#!/usr/bin/env bash

luarocks_version="3.11.1"
luarocks_hash="db2988ee6424337571701f3ec7c8e221"

set -e -u -x
source ./tasks/helpers.sh

task_working_dir
download "http://luarocks.org/releases/luarocks-$luarocks_version.tar.gz" "md5" "$luarocks_hash"
extract_download "luarocks-$luarocks_version.tar.gz"

cd "luarocks-$luarocks_version"
./configure \
  --prefix="$INSTALL_PREFIX_EMBEDDED/openresty/luajit" \
  --with-lua="$STAGE_EMBEDDED_DIR/openresty/luajit"
make
# Clean old installations on version upgrades.
rm -rf "$STAGE_DIR$INSTALL_PREFIX_EMBEDDED/openresty/luajit/share/lua/5.1/luarocks" \
  "$STAGE_DIR$INSTALL_PREFIX_EMBEDDED/openresty/luajit/bin/luarocks"* \
  "$STAGE_DIR$INSTALL_PREFIX_EMBEDDED/openresty/luajit/etc/luarocks"
make install DESTDIR="$STAGE_DIR"
(cd "$STAGE_EMBEDDED_DIR/bin" && ln -snf ../openresty/luajit/bin/luarocks ./luarocks)

stamp
